<?php
namespace app\home\controller;

use app\home\model\Teacher;
use app\home\model\Text;
use app\home\model\User;
use app\home\model\Apply;
use Think\App;
use think\Request;
use think\Session;
class  My extends Base
{
    public $tid = "";
    public function __construct(){
      $this->tid = Session::get("teacher_n")['id'];
    }

    public function index(){
        $user = $this->user_info();
        $apply = Apply::where(["userid"=>$user['id']])->find();
        $teacher = $apply['teacher_id'] != null ? Teacher::get($apply['teacher_id'])->toArray() : null;

        $data = array(
            "setting" => $this->getAllSetting(),
            "user"=>$user,
            "teacher" => $teacher,
            "tid"=>$this->tid,
        );
        return view("index",$data);
    }
    public function info(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
            "user"=>$user,
            "tid"=>$this->tid,
        );
        return view("info",$data);
    }

    public function apply(){
        $user = $this->user_info();
        $Applys = collection(Apply::where(array("userid"=>$user['id']))->select())->toArray();
        if($Applys) {
          $index = count($Applys) - 1;
          $teacher = $Applys[$index]['teacher_id'] != null ? Teacher::get($Applys[$index]['teacher_id'])->toArray() : null;
          $data = array(
            "setting" => $this->getAllSetting(),
            "qrcode" => $this->getSetting("teacher_qrcode"),
            "apply" => $Applys,
            "teacher" => $teacher,
            "tid"=>$this->tid
          );
          return view("apply", $data);
        }else{
          $this->redirect("My/register",["tid"=>$this->tid]);
        }
    }


    /*
     * 我要报名的表单
     */
    public function register(){
       $user = $this->user_info();
        if(empty($user['id'])){
          $this->redirect("Login/logins");
        }
        if (apply::where("userid={$user['id']}")->find()){
          $this->redirect("My/apply");
        }
        $arr = [
          'data'=>$user,
          "setting" => $this->getAllSetting(),
          "tid"=>$this->tid,
        ];
       return view('register',$arr);
    }
    /*
     * 保存报名信息
     */
    public function saveRegister(Request $request){
      $setting = $this->getAllSetting();
      //判断数据库中user表的数据是否完整，如果不完整补充完整
      $user = user::where(['id'=>$this->user_info()['id']])->find();
      $data =$request->post();
      if($user['name'] == ""){ user::update(['name'=>$data['name'],'id'=>$user['id']]);}
      if($user['sex'] == ""){ user::update(['sex'=>$data['sex'],'id'=>$user['id']]);}
      if($user['phone'] == ""){ user::update(['phone'=>$data['phone'],'id'=>$user['id']]);}
      if($user['nation'] == ""){ user::update(['nation'=>$data['nation'],'id'=>$user['id']]);}
      if($user['qqwx'] == ""){ user::update(['qqwx'=>$data['qqwx'],'id'=>$user['id']]);}
      if($user['parenttel'] == ""){ user::update(['parenttel'=>$data['parenttel'],'id'=>$user['id']]);}
      if($user['schooladdress'] == ""){ user::update(['schooladdress'=>$data['schooladdress'],'id'=>$user['id']]);}
      if($user['schoolname'] == ""){ user::update(['schoolname'=>$data['schoolname'],'id'=>$user['id']]);}
      if($user['score'] == ""){ user::update(['score'=>$data['score'],'id'=>$user['id']]);}
      if($user['address'] == ""){ user::update(['address'=>$data['address'],'id'=>$user['id']]);}
      if($user['update_time'] == ""){ user::update(['update_time'=>time(),'id'=>$user['id']]);}
      if($user['grade'] == ""){ user::update(['grade'=>$data['grade'],'id'=>$user['id']]);}
      user::update(['grade'=>date("Y",time()),"id"=>$user['id']]);
      //查询是否有未处理完的报名信息，如果有不允许报名
      if (apply::where("userid={$user['id']}")->find()){
          $this->error("报名失败，您已有报名信息！","My/apply");
      }else {
        $teacher_name =  Session::has("teacher_n") ? Session::get("teacher_n")['name'] : "";
        $teacher_id = Session::has("teacher_n") ? Session::get("teacher_n")['id'] : "";
        //添加报名记录
        $res = apply::create([
            'userid' => $user['id']
          , 'major' => $data['major']
          , 'major_id' => $data['major_id']
          , 'major2' => $data['major2']
          , 'major2_id' => $data['major2_id']
          , 'isadjust' => $data['isadjust']
          , 'bktype' => $data['bktype']
          , 'bktype_id' => $data['bktype_id']
          , 'coursetype_name' => $data['coursetype_name']
          , 'coursetype_id' => $data['coursetype_id']
          , 'coursetype_name2' => $data['coursetype_name2']
          , 'coursetype_id2' => $data['coursetype_id2']
          , 'create_time' => time()
          , 'comment' => $data['comment']
          , 'teacher' =>$teacher_name
          , 'teacher_id' =>$teacher_id
          , 'grade' => $user['grade']
          , 'status' => 5
          , 'type' => 5
        ]);

        if ($res) {
          $teacher_name = empty($teacher_name) ? "未选择":$teacher_name;
          $res = message($data['phone'],["name"=>$data['name']],1); //发送录取通知短信
          message($setting['message_tel'],["name"=>$data['name'],"teacher"=>$teacher_name],7); //发短信通知管理员老师
          $msg_msg = "报名成功，等待报名审核";
           if($res['stat'] == 100){
               $msg_msg .= ",请查收短信通知";
           }else{
               $msg_msg .= "，短信发送失败：".$res['message'];
           }
          $this->success($msg_msg, "My/apply");
        } else {
          $this->error("报名失败，请联系工作人员", "");
        }
      }
    }
    /*
     * 显示付款的二维码页面
     */
    public function pay($id){
      $data = array(
        "setting" => $this->getAllSetting(),
        "id"=>$id,
        "tid"=>$this->tid,
      );
      return view("pay",$data);
    }
    /*
     * 完成支付
     */
    public function finishPay(Request $request){
      $data = $request->post();
      $data['status']=1;
      $data['type']=1;
      $data['pay_time']=time();
      $res = Apply::update($data);
      if ($res) {
        $this->success("操作成功，等待核对", "My/apply");
      } else {
        $this->error("操作失败，请联系工作人员", "Indexs/index");
      }
    }
    /*
     * 录取查询
     */
    public function search(Request $request,$tid="null"){
      $this->teacher_session($tid);
      $apply = "";
      $data="";
      if($request->isPost()) {
        $data = $request->post();
        $res = User::where(["name" => $data['name'], "idcard" => $data['idcard']])->find();
        $apply = Apply::where(['userid' => $res['id']])->select();
      }
      $data = array(
        "setting" => $this->getAllSetting(),
        "apply"=>$apply,
        "data"=>$data,
        "tid"=>$tid,
      );
      return view("search",$data);
    }
    /*
     * 须知
     */
    public function know($k,$tid="null"){
      $this->teacher_session($tid);
      $res= Text::where(["k"=>$k])->find();
      $data = array(
        "setting" => $this->getAllSetting(),
        'text'=>$res,
        "tid"=>$this->tid,
      );
      return view("know",$data);
    }
    /*
     * 通知书
     */
    public function tzs($id){
      $apply = Apply::get($id);
      $data = array(
        "setting" => $this->getAllSetting(),
        'text'=>$this->getAllText(),
        "apply"=>$apply,
        "user"=>User::get($apply['userid']),
        "tid"=>$this->tid,
      );
      return view('tzs',$data);
    }

    //我的考勤
    public function attendance(){
      $user = $this->user_info();
      $data = array(
        "setting" => $this->getAllSetting(),
        "user"=>$user,
        "tid"=>$this->tid,
      );
      return view("attendance",$data);
    }

}
